//
//  GlobalData.m
//  EcCommon
//
//  Created by wuxian on 13-11-21.
//  Copyright (c) 2013年 wuxian. All rights reserved.
//

#import "GlobalData.h"
#import "APService.h"

@interface GlobalData () {
    UIWebView *phoneCallWebView;
}
@end

@implementation GlobalData
static GlobalData* sharedObj = nil;

+ (id)sharedGlobalData {
    @synchronized (self)
    {
        if (sharedObj == nil)
        {
            sharedObj=[[self alloc] init];
        }
    }
    return sharedObj;
}

- (id)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)getData {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userinfo"]!=nil) {
        NSError *error = nil;
        
        id datadic = [NSJSONSerialization
                      
                      JSONObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"userinfo"]
                      
                      options:NSJSONReadingMutableLeaves
                      
                      error:&error];
        [[GlobalData sharedGlobalData] setUserinfoDic:datadic];
    }
    if ([[GlobalData sharedGlobalData] userinfoDic] != nil){
        [APService setAlias:[[GlobalData sharedGlobalData] userinfoDic][@"UserID"] callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
    }

}

- (void)clearData{
    [APService setAlias:@"" callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userinfo"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[GlobalData sharedGlobalData] setUserinfoDic:nil];
}

+ (NSMutableDictionary *)paramterDic {
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithDictionary:@{@"api_key":[[@"1234567890!@#" md5HexDigest] uppercaseString]}];
    return  parameters;
}

+ (NSAttributedString *)getAttriStrWithStr1:(NSString *)str1 str2:(NSString *)str2 str3:(NSString *)str3 {
    UIColor *color1 = [UIColor colorWithRed:255/255.0 green:78/255.0 blue:0/255.0 alpha:1.0];
    NSDictionary *attribs = @{NSForegroundColorAttributeName: color1};
    
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:@""];
    
    NSAttributedString *str_1 = [[NSAttributedString alloc] initWithString:str1];
    [attriStr appendAttributedString:str_1];
    
    NSAttributedString *str_2 = [[NSAttributedString alloc] initWithString:str2 attributes:attribs];
    [attriStr appendAttributedString:str_2];
    
    NSAttributedString *str_3 = [[NSAttributedString alloc] initWithString:str3];
    [attriStr appendAttributedString:str_3];
    
    return attriStr;
}

+ (BOOL)validateUserName:(NSString *)str
{
    NSString *patternStr = [NSString stringWithFormat:@"^[A-Za-z0-9\u4E00-\u9FA5]{5,16}$"];
    NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
                                              initWithPattern:patternStr
                                              options:NSRegularExpressionCaseInsensitive
                                              error:nil];
    NSUInteger numberofMatch = [regularexpression numberOfMatchesInString:str
                                                                  options:NSMatchingReportProgress
                                                                    range:NSMakeRange(0, str.length)];
    if(numberofMatch > 0)
    {
        return YES;
    }
    return NO;
}

//校验用户密码
+ (BOOL) validateUserPasswd:(NSString *)str
{
    NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
                                              initWithPattern:@"^[a-zA-Z0-9]{6,16}$"
                                              options:NSRegularExpressionCaseInsensitive
                                              error:nil];
    NSUInteger numberofMatch = [regularexpression numberOfMatchesInString:str
                                                                  options:NSMatchingReportProgress
                                                                    range:NSMakeRange(0, str.length)];
    
    if(numberofMatch > 0)
    {
        NSLog(@"%@ isNumbericString: YES", str);
        return YES;
    }
    
    NSLog(@"%@ isNumbericString: NO", str);
    return NO;
}

//利用正则表达式验证
+ (BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

//ios处理判断手机号码的正则表达式，基本上包含全部的手机号段
+ (BOOL)checkTel:(NSString *)str {
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0-9]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:str];
}

//ios处理判断手机号码和固话的正则表达式
+ (BOOL)isValidateMobileOrTel:(NSString *)str {
    NSString *regex = @"[0-9-]{7,18}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:str];
}
//在- (void)getData方法的结尾添加

+ (UIColor *)fontColor {
//    return [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
    return [UIColor whiteColor];
}

+ (BOOL)APCheckIfAppInstalled2:(NSString *)urlSchemes {
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlSchemes]]) {
        return  YES;
    }
    else {
        return  NO;
    }
}

+ (BOOL)locationServicesEnabled {
    if (([CLLocationManager authorizationStatus] >= 3)) {
        NSLog(@"手机gps定位已经开启");
        return YES;
    } else {
        NSLog(@"手机gps定位未开启");
        return NO;
    } 
}

//然后添加方法：
- (void)tagsAliasCallback:(int)iResCode tags:(NSSet*)tags alias:(NSString*)alias {
    NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias);
}

- (void)phoneBtnActWithNum:(NSString *)phoneNum {
    NSLog(@"phoneBtnAct");
    if ([phoneNum length]<=0) {
        return;
    }
    
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];
    if ( !phoneCallWebView ) {
        phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];// 这个webView只是一个后台的容易 不需要add到页面上来  效果跟方法二一样 但是这个方法是合法的
    }
    [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
}

+ (NSString*)getStringValue:(id)value
{
    NSRange range = [[NSString stringWithFormat:@"%@",value] rangeOfString:@"null"];
    
    if (value) {
        if ([value isKindOfClass:[NSString class]]) {
            return value;
        }
        else if ([value isKindOfClass:[NSNumber class]]) {
            return [value stringValue];
        }
        //        else if ([[NSString stringWithFormat:@"%@",value]containsString:@"null"]){
        //            return @"";
        //        }
        else if (range.location != NSNotFound){
            return @"";
        }
        else {
            return @"";
        }
        
    }
    else {
        return @"";
    }
}
@end
